import tkinter as tk
from tkinter import ttk

# 创建主窗口
root = tk.Tk()
root.title("Treeview 过滤示例")

# 创建Treeview控件
tree = ttk.Treeview(root, columns=('Name', 'Description'), show='headings')
tree.heading('Name', text='名称')
tree.heading('Description', text='描述')

# 插入一些数据到Treeview
data = [
    ('项目1', '描述1'),
    ('项目2', '这是第二个项目'),
    ('项目3', '这是第三个项目描述'),
    # ... 添加更多数据
]
for item in data:
    tree.insert('', 'end', values=item)

# 创建过滤函数
def filter_tree():
    query = search_var.get().lower()
    for child in tree.get_children():
        if query in tree.item(child)['values'][0].lower() or query in tree.item(child)['values'][1].lower():
            tree.item(child, open=True)
            tree.reattach(child, '', 'end')
        else:
            tree.detach(child)

# 创建一个变量来存储过滤条件
search_var = tk.StringVar()

# 创建一个输入框用于输入过滤条件
search_entry = ttk.Entry(root, textvariable=search_var)
search_entry.pack(fill='x', padx=10, pady=10)

# 创建一个按钮，点击时执行过滤
search_button = ttk.Button(root, text='过滤', command=filter_tree)
search_button.pack(pady=5)

# 布局Treeview
tree.pack(expand=True, fill='both', padx=10, pady=10)

# 启动Tkinter事件循环
root.mainloop()
